package com.fanqwz.hexieshe.presenter;

import android.util.Log;

import com.fanqwz.hexieshe.dagger.PerActivity;
import com.fanqwz.hexieshe.domain.Category;
import com.fanqwz.hexieshe.domain.usercase.GetCategoryUseCase;
import com.fanqwz.hexieshe.mapper.DataMapper;
import com.fanqwz.hexieshe.view.CategoryView;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.observers.DisposableObserver;

/**
 * Created by fanqw on 2017/7/2.
 */

@PerActivity
public class CategoryPresenter implements Presenter {
    private static final String TAG = CategoryPresenter.class.getSimpleName();
    private final GetCategoryUseCase getCategoryUseCase;
    CategoryView categoryView;

    @Inject
    public CategoryPresenter(GetCategoryUseCase getCategoryUseCase) {
        this.getCategoryUseCase = getCategoryUseCase;
    }

    public void setCategoryView(CategoryView categoryView) {
        this.categoryView = categoryView;
    }

    @Override
    public void create() {
    }

    @Override
    public void show() {
        categoryView.prepare();
        getCategoryUseCase.execute(new CategoryObserver(), new GetCategoryUseCase.Params());
    }

    @Override
    public void hide() {
    }

    @Override
    public void destroy() {
    }

    class CategoryObserver extends DisposableObserver<List<Category>> {

        @Override
        public void onNext(List<Category> value) {
            categoryView.showCategorys(DataMapper.transformCategorys(value));
        }

        @Override
        public void onError(Throwable e) {
            Log.e(TAG, "onError: ", e);
        }

        @Override
        public void onComplete() {

        }
    };
}
